package com.fengmi.controller;

import com.fengmi.entity.SysUser;
import com.fengmi.service.IUserService;
import com.fengmi.utils.security.JwtUtils;
import com.fengmi.utils.security.RsaUtils;
import com.fengmi.vo.ResultVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.Map;

/**
 * 作者：陈学虎
 * 时间：2021/7/14 0014 上午 10:20
 */
@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("login")
    @ApiOperation("用户登录")
    private ResultVO login(@RequestBody SysUser user){
        return userService.login(user);
    }

    @GetMapping("verify")
    public ResultVO verify(@RequestHeader("token") String token){

        try {
            if (StringUtils.isEmpty(token)){
                return new ResultVO(false,"令牌为空");
            }

            //解析令牌
            String path = ResourceUtils.getFile("classpath:rsa.pub").getPath();
            PublicKey publicKey = RsaUtils.getPublicKey(path);
            SysUser sysUserToken = (SysUser) JwtUtils.getInfoFromToken((String) token, publicKey, SysUser.class);
            return new ResultVO(true,"令牌有效",sysUserToken);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false,"令牌无效");
        }

    }
}
